import os

from flask import Flask
from flask_pymongo import PyMongo
from flask_sockets import Sockets
from app import models
from config import config

sockets = Sockets()
mongo = PyMongo()

def create_app(config_name=None):
    app = Flask(__name__)
    if config_name is None:
        config_name = os.environ.get('FLACK_CONFIG', 'dev')
    app.config.from_object(config[config_name])

    sockets.init_app(app)

    # regist blueprint
    from app.api.rest import rest
    app.register_blueprint(rest, url_prefix='/api')

    from app.api.websocket import ws
    sockets.register_blueprint(ws, url_prefix='/ws')


    # init mongo
    mongo.init_app(app)

    # print(app.config)
    return app


